package com.tbit.uqbike.webmanager.business.machinegroup.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.uqbike.annos.OperateLog;

import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.webmanager.business.machinegroup.object.dto.MachineGroupAddDto;
import com.tbit.uqbike.webmanager.business.machinegroup.object.dto.MachineGroupUpdateDto;
import com.tbit.uqbike.webmanager.business.machinegroup.object.vo.MachineGroupVo;
import com.tbit.uqbike.webmanager.business.machinegroup.service.MachineGroupService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 车辆分组
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2024/7/12
 */
@Slf4j
@RestController
@RequestMapping("/machineGroup")
@RequiredArgsConstructor
public class MachineGroupController {
    final MachineGroupService machineGroupService;

    /**
     * 查询车辆分组列表（带分组下车辆数量）
     *
     * @param accountId   区域id
     * @param machineType 车辆类型 0-电单车 1-单车
     * @return 区域下所有分组（带分组下车辆数量），machineGroupId为-1的分组为'无分组'（没绑定分组的车辆分组）
     */
    @GetMapping("/list")
    public Result<List<MachineGroupVo>> getListWithCount(@RequestParam Integer accountId, Integer machineType) {
        return Result.success(machineGroupService.selectListWithCount(accountId, machineType));
    }

    /**
     * 添加车辆分组
     *
     * @param dto 分组信息
     */
    @PostMapping("/add")
    @OperateLog(module = ModuleEnum.MACHINE_GROUP, methods = MethodEnum.MACHINE_GROUP_ADD)
    public Result<Void> add(@RequestBody @Validated MachineGroupAddDto dto) {
        machineGroupService.add(dto);
        return Result.success();
    }

    /**
     * 批量修改车辆分组
     *
     * @param dtoList 分组信息
     */
    @PutMapping("/batchUpdate")
    @OperateLog(module = ModuleEnum.MACHINE_GROUP, methods = MethodEnum.MACHINE_GROUP_UPDATE)
    public Result<Void> batchUpdate(@RequestBody @Validated List<MachineGroupUpdateDto> dtoList) {
        machineGroupService.batchUpdate(dtoList);
        return Result.success();
    }

    /**
     * 删除车辆分组
     *
     * @param machineGroupId 分组id
     */
    @DeleteMapping("/delete/{machineGroupId}")
    @OperateLog(module = ModuleEnum.MACHINE_GROUP, methods = MethodEnum.MACHINE_GROUP_DEL)
    public Result<Void> delete(@PathVariable Integer machineGroupId) {
        if (machineGroupId == -1) {
            return Result.error("默认分组不能删除");
        }
        if (machineGroupService.removeById(machineGroupId)) {
            return Result.success();
        }
        return Result.error("分组不存在");
    }
}
